package com.ninong.ker.admin.service.impl;

import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import com.alibaba.nacos.client.config.utils.MD5;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ninong.ker.admin.domain.Dept;
import com.ninong.ker.admin.domain.User;
import com.ninong.ker.admin.domain.UserRole;
import com.ninong.ker.admin.mapper.DeptMapper;
import com.ninong.ker.admin.mapper.UserMapper;
import com.ninong.ker.admin.mapper.UserRoleMapper;
import com.ninong.ker.admin.service.UserService;
import com.ninong.ker.admin.service.dto.UserDTO;
import com.ninong.ker.common.exception.CmdException;
import com.ninong.ker.common.tools.Constants;
import com.ninong.ker.common.tools.enums.BusinessEnum;

import cn.hutool.core.lang.UUID;

/**
 * 
 * @author cmd
 *
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

	private static final String PASSWORD = "cmd.c0m";
	private static final String PARENT = "0";

	@Autowired
	private DeptMapper deptMapper;
	@Autowired
	private UserRoleMapper userRoleMapper;

	@Override
	public PageInfo<User> listPage(UserDTO dto) {
		PageHelper.startPage(dto.getCurrentPage(), dto.getPageSize());
		QueryWrapper<User> query = new QueryWrapper<>();
		if (!StringUtils.isEmpty(dto.getNickName())) {
			query.like("nick_name", dto.getNickName());
		}
		if (!StringUtils.isEmpty(dto.getDeptId())) {
			query.eq("dept_id", dto.getDeptId());
		}
		if (!StringUtils.isEmpty(dto.getUserName())) {
			query.like("user_name", dto.getUserName());
		}
		if (!StringUtils.isEmpty(dto.getPhone())) {
			query.like("phone", dto.getPhone());
		}
		List<User> users = baseMapper.selectList(query);
		return new PageInfo<User>(users);
	}

	@Override
	public void delete(String[] ids) {
		List<String> idList = Arrays.asList(ids);
		idList.forEach(id -> {
			Integer count = userRoleMapper.selectCount(new QueryWrapper<UserRole>().eq("user_id", id));
			if (count > Constants.ZERO) {
				throw new CmdException(BusinessEnum.USER_ROLE_EXIST);
			}			
			User entity = new User();
			entity.setId(id);
			entity.setDeleted(false);
			baseMapper.updateById(entity);
		});
	}

	@Override
	public void create(User entity) throws Exception {
		if (StringUtils.isEmpty(entity.getId())) {
			String salt = UUID.fastUUID().toString();
			entity.setSalt(salt);
			String password = MD5.getInstance().getMD5String(salt + PASSWORD);
			entity.setPassword(password);
		}
		Integer count = null;
		if (StringUtils.isEmpty(entity.getId())) {
			count = baseMapper.selectCount(new QueryWrapper<User>().eq("user_name", entity.getUserName()));
			if (count > Constants.ZERO) {
				throw new CmdException(BusinessEnum.USER_NAME_REPEAT);
			}
			count = baseMapper.selectCount(new QueryWrapper<User>().eq("phone", entity.getPhone()));
			if (count > Constants.ZERO) {
				throw new CmdException(BusinessEnum.USER_PHONE_REPEAT);
			}
			count = baseMapper.selectCount(new QueryWrapper<User>().eq("email", entity.getEmail()));
			if (count > Constants.ZERO) {
				throw new CmdException(BusinessEnum.USER_EMAIL_REPEAT);
			}
		} else {
			count = baseMapper.selectCount(
					new QueryWrapper<User>().ne("id", entity.getId()).eq("user_name", entity.getUserName()));
			if (count > Constants.ZERO) {
				throw new CmdException(BusinessEnum.USER_NAME_REPEAT);
			}
			count = baseMapper
					.selectCount(new QueryWrapper<User>().ne("id", entity.getId()).eq("phone", entity.getPhone()));
			if (count > Constants.ZERO) {
				throw new CmdException(BusinessEnum.USER_PHONE_REPEAT);
			}
			count = baseMapper
					.selectCount(new QueryWrapper<User>().ne("id", entity.getId()).eq("email", entity.getEmail()));
			if (count > Constants.ZERO) {
				throw new CmdException(BusinessEnum.USER_EMAIL_REPEAT);
			}

		}
		saveOrUpdate(entity);
	}

	@Override
	public User get(String id) {
		User entity = baseMapper.selectById(id);
		if (ObjectUtils.isEmpty(entity)) {
			entity = new User();
		}
		Dept dto = new Dept();
		dto.setPid(PARENT);
		entity.setResouces(deptMapper.selectDeptTree(dto));
		return entity;
	}

	@Override
	public void enableUser(String id, Boolean enable) {
		User entity = new User();
		entity.setId(id);
		entity.setEnabled(enable);
		baseMapper.updateById(entity);
	}

	@Override
	public List<User> listUserTree() {
		LambdaQueryWrapper<User> query = new LambdaQueryWrapper<>();
		query.eq(User::getDeleted,Constants.DEFAULT_YES);
		query.select(User::getId,User::getNickName);
		List<User> users = baseMapper.selectList(query);
		return users;
	}

}
